package com.wx.activitys.base.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.wx.activitys.base.utils.Common;
import sun.misc.BASE64Decoder;
@Controller
@RequestMapping(value = "/file")
public class FileController extends BaseController{
	
	
	@RequestMapping("/getImg.html")  
    public void getimg(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String saveAddress=request.getParameter("saveAddress");
        try{
        	saveAddress = Common.IMG_SAVE_BASEPATH+saveAddress;
            FileInputStream hFile = new FileInputStream(saveAddress); // 以byte流的方式打开文件 d:\1.gif  
            int i=hFile.available(); //得到文件大小  
            byte data[]=new byte[i];  
            hFile.read(data); //读数据  
            hFile.close();  
            response.setContentType("image/*"); //设置返回的文件类型  
            OutputStream toClient=response.getOutputStream(); //得到向客户端输出二进制数据的对象  
            toClient.write(data); //输出数据  
            toClient.close();  
        }  
        catch(IOException e) //错误处理  
        {
            e.printStackTrace();
        	PrintWriter toClient = response.getWriter(); //得到向客户端输出文本的对象  
            response.setContentType("text/html;charset=gb2312");  
            toClient.write("无法打开图片!");  
            toClient.close();  
        }   
    } 
	/**
	 * 接收上传图片
	 * @throws IOException 
	 */
	@RequestMapping(value="/upload.html",method={RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody Object uploadImg(String userfile,String imgDirPath, HttpServletRequest request, HttpServletResponse response) throws IOException{
		//PrintWriter out = response.getWriter();

		// 获取参数
		String imgData = userfile;
		System.out.println(imgData);
		// 绝对路径
		String path = Common.IMG_SAVE_BASEPATH+imgDirPath;
		System.out.println(path);
		File targetFile = new File(path);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}

		// 生成新的图片名称（入库）
		//String imgSuffix = imgData.split(",")[0];
		String fileName = UUID.randomUUID().toString().replaceAll("-", "");

		//boolean flag = GenerateImage(imgData, fileName, path);
		
		//out.print(flag);
		//out.flush();
		//out.close();
		return fileName;
	}
	// 将base64字符文件输出图像
		public static boolean GenerateImage(String imgStr, String fileName,	String path) {
			// 对字节数组字符串进行Base64解码并生成图片
			if (imgStr == null) // 图像数据为空
				return false;
			BASE64Decoder decoder = new BASE64Decoder();
			try {
				// Base64解码
				byte[] b = decoder.decodeBuffer(imgStr);
				for (int i = 0; i < b.length; ++i) {
					if (b[i] < 0) {// 调整异常数据
						b[i] += 256;
					}
				}
				// 生成jpeg图片
				String imgFilePath = path + "/" + fileName+".jpg";// 新生成的图片
				OutputStream out = new FileOutputStream(imgFilePath);
				out.write(b);
				out.flush();
				out.close();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
		}
}
